import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import SearchForm from '#cpt/temp/SearchForm'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		forms : [
			{ label: '批号',		name: 'lost_id', 			type: 'select', loading:true },
			{ label: '日期',		names: ['start','end'], 	name:'jz-time', type:'date-range' },
		],
	}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['30']()
			this.resetModel = { start: arr[0], end: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.init()
		})
	}
	
	init = () => {
		const { lot_id, uuid } = this.props.data
		this.uuid = uuid
		this.lot_id = lot_id
		this.fetch()
	}
	
	fetch = () => $fn.fetch.call(this,'qc-data/stat', {paging: 'get', param: {uuid: this.uuid, lot_id: this.lot_id}, isIndex: 1, callback: data => this.setCols(data)})
	
	getSelect = async () => {
		const { forms } = this.state
		const data = await $http.pull(null, 'qc-lot/select')
		$fn.setRow(forms, 'lost_id', data )
		this.setState({ forms })
	}
	
	setCols = ({level}) => {
		this.cols = [
			{ title: '',	children: [
				{ title: '',  			dataIndex: 'index', 		width: 40,	 render: text => text + 1 },
				{ title: '日期和时刻',	dataIndex: 'entered_at', 	...$fn.table.time },
				{ title: '测试',			dataIndex: 'name', 	...$fn.table.time }
			] }
		]
		for(let i = 1; i <= level; i++){
			this.cols.push(
				{ title: `水平${i}`,	children: [
					{ title: '标准差',		dataIndex: `level${i}_sd`, 		width: 60 },
					{ title: '平均值',		dataIndex: `level${i}_avg`,  	width: 60 },
					{ title: '变异系数',		dataIndex: `level${i}_cv`, 		width: 100 },
					{ title: '靶值',		dataIndex: `level${i}_z`, 		width: 100 },
				] }
			)
		}
		this.table?.table.updateCol(this.cols)
	}
	
	render(){
		return <div className='fv ex'>
			{ SearchForm.call(this, { onForm: ref => {
				$time.then( f => ref.setValue({ 'jz-time': f.duration['30'](), lost_id: this.lot_id }) )
				ref.submit()
			} }) }
			{ Table.call(this, {isIndex: 1, onRow: ({test_id}) => {
				this.props.chartRef.init({test_id, start: this.model.start, end: this.model.end})
			}}) }
		</div>
	}
}

export default Index